package com.example.graduationdesignbackend.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.example.graduationdesignbackend.constants.ResponseCode;
import com.example.graduationdesignbackend.dto.PageDto;
import com.example.graduationdesignbackend.entity.News;
import com.example.graduationdesignbackend.query.NewsQuery;
import com.example.graduationdesignbackend.res.Res;
import com.example.graduationdesignbackend.service.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuze
 * @since 2024-04-03
 */
@RestController
@CrossOrigin
@SaCheckLogin
@SaCheckRole("super-admin")
@RequestMapping("/news")
public class NewsController {
  @Autowired
  private INewsService newsService;
  @GetMapping()
  public News getNewsInfo(String id){
    News news = newsService.getById(id);
    return news;
  }

  @PostMapping()
  public Res createNews(@RequestBody News news){
    newsService.createNews(news);
    return Res.yuze();
  }

  @PutMapping()
  public Res editNews(@RequestBody News news){
    return newsService.editNews(news);
  }

  @DeleteMapping
  public Res deleteNews(String id){
    Boolean delete = newsService.removeById(id);
    if (!delete) return Res.fail(ResponseCode.Fail);
    return Res.yuze();
  }

  @GetMapping("/page")
  public Res<PageDto<News>> getNewsPages(NewsQuery newsQuery){
    return Res.yuze(newsService.getNewsPages(newsQuery));
  }
}
